<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Demo</title>
	<link href="../css/layui.css" rel="stylesheet" media="all">
</head>
<body style="padding-left: 20px;">
<br>
<div class="demoTable" id="yc-add">
	<label>姓名</label>
	<div class="layui-input-inline">
		<input type="text" name="aname" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input"/>
	</div>
	<label>手机号</label>
	<div class="layui-input-inline">
		<input type="text" name="tel" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input"/>
	</div>
	<button type="button" class="layui-btn layui-btn-warm" onclick="add()">添加</button>
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="barDemo">
	<div class="layui-clear-space">
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
	</div>
</script>
<script src="../js/jquery-3.4.1.min.js"></script>
<script src="../js/layui.js" charset="utf-8"></script>
<script src="../js/axios.js"></script>
<script>
	layui.use('table',function(){
		var table=layui.table;
		table.render({
			elem:'#test',
			url:'../../admin?op=findPage',
			toolbar:'default',
			title:'管理员信息表',
			cellMinWidth:120,
			id:'test',
			page:true,
			page:{limits:[3,5,7]},

			cols:[[
				{type:'checkbox',fixed:'center'},
				{field:'aid',title:'编号',fixed:'center',unresize:true,sort:true,align:'center'},
				{field:'aname',title:'用户名',fixed:'center',align:'center'},
				{field:'pwd',title:'密码',fixed:'center',align:'center'},
				{field:'tel',title:'联系方式',fixed:'center',align:'center'},
				{field:'aa',title:'操作',templet:"#barDemo"}

			]]
		});
		table.on('toolbar(test)',function(obj){
			var checkStatus=table.checkStatus(obj.config.id),data=checkStatus.data;
			switch(obj.event){
				case 'add':
					layer.msg('添加');
					break;
				case 'update':
					console.log(data);
					break;
				case 'delete':
					layer.confirm('数据一但删除不可恢复，您确定删除吗？',{icon:3,title:'确认'},function(index){
						var arr =[];
						for(var i = 0; i<data.length;i++){
							arr.push(data[i].id);
						}
						console.log(arr);
						layer.close(index);
						table.reload('test',{
							where:{id:1002,status:2},
							page: {
								curr:1
							}
						});
					})
					break;

			}
		});
		table.on('tool',function (obj) {
			var data=obj.data;
			console.log(obj);
			if (obj.event=='edit'){
				alert('edit');
			}else if (obj.event=='del'){
				axios.get("menber", {
					params: {
						op: 'del',
						aname: this.aname,
					}
				}).then(rs => {
					console.log(rs.data);
					if(rs.data>0){
						alert("删除成功");
						location.href='admin.html';

					}else {
						alert('删除失败!!');
					}
				});


				alert('del');
			}
		})
	});



	function add() {
		var aname=$('#yc-add input[name="aname"]').val();
		var tel=$('#yc-add input[name="tel"]').val();
		$.post("../../admin",{aname:aname,tel:tel,op:'add'},function (data) {

			if (data>0){
				layer.msg('添加成功');
			} else{
				layer.alert('添加失败',{icon:0});
			}
		});
	}
</script>
</body>
</html>